from django.db import models
from lib.CommonModels import CommonModel


class Article(CommonModel):
    '''
    存放文章表
    字段：标题，简介，具体内容，点赞数，点踩数，评论数
    外键字段：站点，分类，标签
    '''
    # 信息
    title = models.CharField(max_length=255, verbose_name='文章标题')
    desc = models.CharField(max_length=255, verbose_name='文章简介')
    content = models.TextField(verbose_name='文章正文')
    # 计数
    up_count = models.BigIntegerField(default=0, verbose_name='点赞数')
    down_count = models.BigIntegerField(default=0, verbose_name='点踩数')
    comment_count = models.BigIntegerField(default=0, verbose_name='评论数')
    # 外键
    # 文章与站点 【一对多】 一个 个人站点 可以有多个文章
    blog = models.ForeignKey(to='app_blog.Blog', on_delete=models.CASCADE, verbose_name='文章关联的站点')
    # 文章与分类 【一对多】 一篇文章只能出现在一个分类中
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE, verbose_name='文章关联的分类', null=True)
    # 文章与标签 【多对多】 一篇文章可以有多个标签 一个标签下可以有多个文章
    tags = models.ManyToManyField(to='Tags', verbose_name='文章关联标签')
    is_delete = models.BooleanField(default=False, verbose_name='是否被删除')

    class Meta():
        db_table = 'article_info'
        verbose_name_plural = '文章信息表'

    def __str__(self):
        return f"{self.title} "


class Category(models.Model):
    name = models.CharField(max_length=32, verbose_name='文章分类')
    # 站点与文章 【一对多】 一个个人站点可以有多个分类
    blog = models.ForeignKey(to='app_blog.Blog', on_delete=models.CASCADE)

    class Meta():
        db_table = 'article_category'
        verbose_name_plural = '文章分类表'

    def __str__(self):
        return f"{self.name} "


class Tags(models.Model):
    name = models.CharField(max_length=32, verbose_name='标签')
    blog = models.ForeignKey(to='app_blog.Blog', on_delete=models.CASCADE)

    class Meta():
        db_table = 'article_tags'
        verbose_name_plural = '文章标签表'

    def __str__(self):
        return f"{self.name} "


class UpOrDown(CommonModel):
    '''
    CommonModel : 创建时间，更新时间
    字段：点赞或点踩，用户，文章
    '''
    up_down = ((True, '赞'), (False, '踩'))
    is_up = models.BooleanField(choices=up_down, verbose_name='点赞或点踩')
    user = models.ForeignKey(to='app_user.UserInfo', on_delete=models.CASCADE, verbose_name='点赞用户')
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE, verbose_name='点赞的文章')

    class Meta():
        db_table = 'article_UpOrDown'
        verbose_name_plural = '文章的点赞信息'


class Comment(CommonModel):
    '''
    CommonModel : 创建时间，更新时间
    字段：评论内容，评论文章，评论用户，子评论
    '''
    content = models.TextField(verbose_name='评论正文')
    user = models.ForeignKey(to='app_user.UserInfo', on_delete=models.CASCADE, verbose_name='评论用户')
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE, verbose_name='评论文章')
    parent = models.ForeignKey(to="self", null=True, on_delete=models.CASCADE, verbose_name='子评论')

    class Meta():
        db_table = 'article_comment'
        verbose_name_plural = '文章评论表'
